package com.example.zx.theline.lyjh.lyrjh.pub;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zx.theline.R;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import GsonBean.SgcxrjhBean;
import GsonBean.SgcxrjhDetailBean;
import GsonBean.SgrjhCkshjlBean;
import GsonBean.SgrjhShListBean;
import Utils.MyToast;
import app.MyApplication;
import base.BaseActivity;
import network.NetWorkApi;
import network.ObjectHttpResponseHandler;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;

/**
 * Created by Jianqi on 2017\9\14 0014.
 * <p>
 * 创建施工日计划查看审核记录页面
 */

public class SgrjhCkshjlActivity extends BaseActivity implements View.OnClickListener {
    //UI
    private StickyListHeadersListView mListView;
    private TextView leftTextView,centerTextView,rightTextView;
    private TextView cancel;
    //Data
    private List<SgrjhCkshjlBean.ShdwListBean> list;
    private SgrjhCkshjlBean mSgrjhCkshjlBean;
    private MyAdapter mAdapter;
    //从详情页面传递过来的数据
    public static SgcxrjhDetailBean detailBean;
    public static SgrjhShListBean.RowsBean mRowsBean;
    public static SgcxrjhBean.RowsBean mBean;
    private String flags = "";
    private String flag = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_info);
        flags = getIntent().getStringExtra("flags");
        flag = getIntent().getStringExtra("flag");
        if (flags == null) {
            flags = "";
        }
        if (flag == null) {
            flag = "";
        }
        InitView();
        InitListen();
        InitData();
    }

    @Override
    protected void InitView() {
        mListView = (StickyListHeadersListView) findViewById(R.id.listview);
        leftTextView = (TextView) findViewById(R.id.leftTextView);
        centerTextView = (TextView) findViewById(R.id.centerTextView);
        rightTextView = (TextView) findViewById(R.id.rightTextView);
        cancel = (TextView) findViewById(R.id.cancel);
        centerTextView.setText("查看审核记录");
        rightTextView.setVisibility(View.INVISIBLE);
    }

    @Override
    protected void InitData() { //初始化数据
        requestSgzjhCkshjlData();
    }

    @Override
    protected void InitListen() {
        leftTextView.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.leftTextView:
                finish();
                break;
            case R.id.cancel:
                finish();
                break;
        }
    }

    //请求施工周计划查看审核记录的数据
    private void requestSgzjhCkshjlData() {
        Map<String,String> map=new HashMap<>();
        if (flags.equals("flags")) {
            map.put("jdjhbBh", mRowsBean.getBh());
            map.put("cjrId", mRowsBean.getCjrId());
        } else if (flag.equals("flag")) {
            map.put("jdjhbBh", mBean.getBh());
            map.put("cjrId", mBean.getCjrId());
        } else {
            map.put("jdjhbBh", detailBean.getInfo().getBh());
            map.put("cjrId", detailBean.getInfo().getCjrId());
        }
        MyApplication.ShowWaitDialog(this);
        NetWorkApi.requestSgrjhCkshjlData(map, new Callback<SgrjhCkshjlBean>() {
                    @Override
                    public void onResponse(Call<SgrjhCkshjlBean> call, Response<SgrjhCkshjlBean> response) {
                        try {
                            mSgrjhCkshjlBean = response.body();
                            if (mSgrjhCkshjlBean.getMsg().equals("success")) {
                                if (mSgrjhCkshjlBean.getShdwList().size() > 0) {
                                    list = mSgrjhCkshjlBean.getShdwList();
                                    if (mAdapter == null) {
                                        mAdapter = new MyAdapter();
                                        mListView.setAdapter(mAdapter);
                                    }
                                } else {
                                    Toast.makeText(SgrjhCkshjlActivity.this, "暂无数据", Toast.LENGTH_SHORT).show();
                                }
                            } else {
                                MyToast.showNullObjectError(SgrjhCkshjlActivity.this);
                            }
                        } catch (Exception e) {
                            MyToast.showFail();
                        }
                        MyApplication.HideWaitDialog();
                    }

                    @Override
                    public void onFailure(Call<SgrjhCkshjlBean> call, Throwable t) {
                        MyToast.showFail();
                    }
                }
        );
    }

    //创建施工日计划查看审核记录的适配器
    private class MyAdapter extends BaseAdapter implements StickyListHeadersAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder mHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(SgrjhCkshjlActivity.this).inflate(R.layout.adapter_check_info, null);
                mHolder = new ViewHolder();
                mHolder.sgdwKey = (TextView) convertView.findViewById(R.id.sgdw_key);
                mHolder.sgdw = (TextView) convertView.findViewById(R.id.sgdw_value);
                mHolder.isfouty = (TextView) convertView.findViewById(R.id.isnot_agree_value);
                mHolder.shyj = (TextView) convertView.findViewById(R.id.csshyj_value);
                mHolder.riqi = (TextView) convertView.findViewById(R.id.date_time_value);
                mHolder.tbrLayout = (LinearLayout) convertView.findViewById(R.id.tbr_layout);
                mHolder.tbr = (TextView) convertView.findViewById(R.id.tbr_value);
                mHolder.ckdwlxr = (TextView) convertView.findViewById(R.id.scan_contact);
                mHolder.cklxr_layout = (LinearLayout) convertView.findViewById(R.id.cklxr_layout);
                mHolder.view = convertView.findViewById(R.id.view1);
                convertView.setTag(mHolder);
            } else {
                mHolder = (ViewHolder) convertView.getTag();
            }
            //隐藏查看单位联系人这行
            mHolder.cklxr_layout.setVisibility(View.GONE);
            //根据条件判断显示不同审核类型
            if (list.get(position).getDwlb() != null) {
                switch (list.get(position).getDwlb()) {
                    case "sgdw":
                        mHolder.sgdwKey.setText("施工单位:");
                        break;
                    case "jldw":
                        mHolder.sgdwKey.setText("监理单位:");
                        break;
                    case "jsdw":
                        mHolder.sgdwKey.setText("建设单位:");
                        break;
                    case "cwzd":
                        mHolder.sgdwKey.setText("车务站段:");
                        break;
                    case "sbgldw":
                        mHolder.sgdwKey.setText("设备管理单位:");
                        break;
                    case "qtdw":
                        mHolder.sgdwKey.setText("其他单位:");
                        break;
                    case "jsglbm":
                        mHolder.sgdwKey.setText("建设管理部门:");
                        break;
                    case "shcs":
                        mHolder.sgdwKey.setText("审核处室:");
                        break;
                    case "spcs":
                        mHolder.sgdwKey.setText("项目主管部门:");
                        break;
                    case "sgb":
                        mHolder.sgdwKey.setText("施工办:");
                        break;
                    case "tgdw":
                        mHolder.sgdwKey.setText("停工批准单位:");
                        break;
                    default:
                        break;
                }
            }
            //单位名称
            mHolder.sgdw.setText(list.get(position).getDwmc());
            //是否通过，根据条件判断显示对应名称
            if (list.get(position).getSftg() != null) {
                switch (list.get(position).getSftg()) {
                    case "Y":
                        mHolder.isfouty.setText("同意");
                        break;
                    case "N":
                        mHolder.isfouty.setText("不同意");
                        break;
                    case "ZF":
                        mHolder.isfouty.setText("作废");
                        break;
                    default:
                        break;
                }
            } else {
                mHolder.isfouty.setText("未审核");
            }
            //审核意见
            if (list.get(position).getShyj() == null || list.get(position).getShyj().length() == 0) {
                mHolder.shyj.setText("无");
            } else {
                mHolder.shyj.setText(list.get(position).getShyj());
            }
            //创建日期
            if (list.get(position).getShrq() == null || list.get(position).getShrq().length() == 0) {
                mHolder.riqi.setText("无");
            } else {
                mHolder.riqi.setText(list.get(position).getShrq());
            }
            //判断填报人信息是否显示
            String shrInfo = list.get(position).getShrInfo();
            if (shrInfo != null) {
                if (shrInfo.equals("")) {
                    mHolder.tbrLayout.setVisibility(View.GONE);
                    mHolder.view.setVisibility(View.GONE);
                } else {
                    mHolder.tbrLayout.setVisibility(View.VISIBLE);
                    mHolder.view.setVisibility(View.VISIBLE);
                    String[] values = shrInfo.split(","); //按逗号拆分
                    StringBuilder stringBuilder = new StringBuilder();
                    for (String value : values) { //将拆分后的字符串拼接起来
                        stringBuilder.append(value).append("\n");
                    }
                    mHolder.tbr.setText(stringBuilder.toString());
                }
            }
            return convertView;
        }

        @Override
        public View getHeaderView(int position, View convertView, ViewGroup parent) {
            HeaderViewHolder holder;
            if (convertView == null) {
                holder = new HeaderViewHolder();
                convertView = LayoutInflater.from(SgrjhCkshjlActivity.this).inflate(R.layout.item_checkinfo_group, parent, false);
                holder.text = (TextView) convertView.findViewById(R.id.item_text);
                convertView.setTag(holder);
            } else {
                holder = (HeaderViewHolder) convertView.getTag();
            }
            int max = Integer.parseInt(list.get(0).getShzt()); //先把当前最大值取出
            List<Integer> mlist = new ArrayList<>(); //新建源数据集合
            List<Integer> resultList = new ArrayList<>(); //新建反转数据集合
            for (int i = max; i > 0; i--) { //源数据集合整理
                mlist.add(i);
                resultList.add(i);
            }
            Collections.reverse(resultList); //反转结果集
            //展示审核流程记录数据 第一组内容改为 当前审核记录，第二组则 改为历史组别1，依次类推
            if (Integer.parseInt(list.get(position).getShzt()) == max) {
                holder.text.setText("当前审核记录");
            }
            for (int i = 0; i < mlist.size(); i++) {
                if (mlist.get(i) == (Integer.parseInt(list.get(position).getShzt()) + 1)) {
                    holder.text.setText("历史组别" + resultList.get(i));
                }
            }
            return convertView;
        }

        @Override
        public long getHeaderId(int position) {
            return Long.parseLong(list.get(position).getShzt());
        }

        class HeaderViewHolder {
            TextView text;
        }

        //ViewHolder类
        class ViewHolder {
            TextView sgdwKey;
            TextView sgdw; //施工单位
            TextView isfouty; //是否同意
            TextView shyj; //审核意见
            TextView riqi; //日期时间
            LinearLayout tbrLayout; //填报人行
            TextView tbr; //填报人
            TextView ckdwlxr; //查看单位联系人
            View view; //横线
            LinearLayout cklxr_layout;
        }
    }

}
